{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "###  Python内置数据结构列表、切片方法练习"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- 列表 list（有序、可变）"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "```python\n",
    "names = ['Ruihan', 'Zhichao', 'Lisa', 'Jenny']\n",
    "print(names)\n",
    "# ['Ruihan', 'Zhichao', 'Lisa', 'Jenny'\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "names_str = \"Linda,Mike\"\n",
    "names_list = [\"Linda\",\"Mike\"]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- 列表的方法（函数）"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "```python\n",
    "# 1、增加 \n",
    "\n",
    "# append增加在列表末尾\n",
    "names.append('Taylor')\n",
    "names.append('Billie')\n",
    "print(names)\n",
    "# ['Ruihan', 'Zhichao', 'Lisa', 'Jenny', 'Taylor', 'Billie']\n",
    "\n",
    "# insert增加在指定位置\n",
    "names.insert(2, 'Lucky')\n",
    "print(names)\n",
    "# ['Ruihan', 'Zhichao', 'Lucky', 'Lisa', 'Jenny', 'Taylor', 'Billie']\n",
    "```\n",
    "```python\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "```python\n",
    "# 2、删除 \n",
    "\n",
    "# pop()不填，删除列表末尾；填，删除指定位置的列表内容\n",
    "names.pop()\n",
    "print(names)\n",
    "# ['Ruihan', 'Zhichao', 'Lisa', 'Jenny', 'Taylor', 'Billie']\n",
    "pop_names = names.pop(2) # 被删除的内容\n",
    "print(pop_names)\n",
    "print(names)\n",
    "# Ruihan\n",
    "# ['Zhichao', 'Lisa', 'Jenny', 'Taylor', 'Billie']\n",
    "\n",
    "# remove 可以删除指定内容\n",
    "names.remove('Taylor')\n",
    "print(names)\n",
    "# [Zhichao', 'Lisa', 'Jenny',  'Billie']\n",
    "```\n",
    "```python\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "```python\n",
    "# 3、修改（需要清楚知道位置）\n",
    "\n",
    "names[0] = 'Dua'\n",
    "print(names)\n",
    "# ['Dua', 'Hins', 'Kay', 'Joey']\n",
    "```\n",
    "```python\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "```python\n",
    "# 4、查询并进行修改或者其他操作\n",
    "\n",
    "name = 'Lucy'\n",
    "if name in names:\n",
    "    names[names.index(name)] = 'Ruihan'\n",
    "    print(names)\n",
    "else:\n",
    "    print(\"%s 不在我们的列表中\" % (name))\n",
    "# ['Ruihan', 'Hins', 'Kay', 'Joey']\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- 列表的切片"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "```python\n",
    "names = ['Ruihan', 'Hins', 'Kay', 'Joey']\n",
    "print(names[1],names[2])\n",
    "# Hins Kay\n",
    "\n",
    "# 切片（仍然是一个列表）\n",
    "print(names[1:3])\n",
    "# ['Ruihan', 'Kay']\n",
    "\n",
    "# 从列表首位开始，可以省略0\n",
    "print(names[0:3])\n",
    "print(names[:3])\n",
    "# ['Ruihan', 'Hins', 'Kay']\n",
    "# ['Ruihan', 'Hins', 'Kay']\n",
    "\n",
    "# 从列表末位开始\n",
    "print(names[-3:-1])\n",
    "print(names[-3:])\n",
    "# ['Hins', 'Kay']\n",
    "# ['Hins', 'Kay', 'Joey']\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- 其他方法"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "```python\n",
    "#clear 删除所有元素\n",
    "names.clear()\n",
    "print(names)\n",
    "# []\n",
    "\n",
    "# sort 对列表进行排序\n",
    "# count统计字符串里某个字符出现的次数\n",
    "nums = [1, 3, 2, 4, 5, 2, 3, 2, 1]\n",
    "nums.sort()\n",
    "print(nums)\n",
    "print(nums.count(2))\n",
    "# [1, 1, 2, 2, 2, 3, 3, 4, 5]\n",
    "# 3\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 列表的高级用法"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- index()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "```\n",
    "print(names.index(\"mary\"))\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- sort()排序 默认从小到大排序 reverse=false（默认）小->大\n",
    "                              true        大—>小"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "```\n",
    "# stu_成绩.sort()\n",
    "# print(stu_成绩)\n",
    "stu_成绩.sort(reverse=True)\n",
    "print(stu_成绩)\n",
    "names.sort()# 字符串 以首字母按照ASCII排序\n",
    "print(names)\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- count()查看某元素的次数"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "```\n",
    "print(stu_成绩.count(95))\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- clear()清空list"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "```\n",
    "# names.clear()\n",
    "# print(names)\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- extend()是有顺序的，操作哪一个列表，那一个列表就先写"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "```\n",
    "# names.extend(names_j)\n",
    "# print(names)\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 课本代码"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Don't panic!\n",
      "['D', 'o', 'n', \"'\", 't', ' ', 'p', 'a', 'n', 'i', 'c', '!']\n",
      "['o', 'n', ' ', 't', 'a', 'p']\n",
      "on tap\n"
     ]
    }
   ],
   "source": [
    "#   P67\n",
    "phrase = \"Don't panic!\"\n",
    "plist = list(phrase)\n",
    "print(phrase)\n",
    "print(plist)\n",
    "for i in range(4):\n",
    "    plist.pop()\n",
    "plist.pop(0)\n",
    "plist.remove(\"'\")\n",
    "plist.extend([plist.pop(),plist.pop()])\n",
    "plist.insert(2,plist.pop(3))\n",
    "new_phrase = ''.join(plist)\n",
    "print(plist)\n",
    "print(new_phrase)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
